package com.soda.utils;

import com.coreos.jetcd.Auth;
import com.coreos.jetcd.auth.AuthRoleGetResponse;
import com.coreos.jetcd.auth.AuthUserAddResponse;
import com.coreos.jetcd.auth.AuthUserGetResponse;
import com.coreos.jetcd.auth.Permission;
import com.coreos.jetcd.data.ByteSequence;
import com.coreos.jetcd.kv.PutResponse;
import org.junit.Test;

/**
 * Created by life on 2018/3/23.
 */
public class ServerRegisterCreateUserTest_1 {

    @Test
    public void instantiateTest() throws Exception {
        ServerRegister sr = new ServerRegister();
        sr.instantiate(true);
        System.err.println(String.format("初始化完成！"));
    }

    public static void main(String[] args) throws Exception {
        ServerRegister sr = new ServerRegister();
//        sr.setAppName("root");
//        sr.setPassword("123");
        sr.setAppName("appName3");
        sr.setPassword("password3");
        sr.setEtcdUrls("http://192.168.99.100:2379");
//        sr.setEtcdUrls("http://172.24.7.17:8183");
        sr.afterPropertiesSet();
        System.err.println(String.format("初始化完成-1！index:%1s", sr.getIndex()));
        //
        Auth authClient = sr.getClient().getAuthClient();

//        AuthUserGetResponse appName2 = authClient.userGet(ByteSequence.fromString("appName2")).get();
//        AuthUserAddResponse authUserAddResponse = authClient.userAdd(ByteSequence.fromString("appName3"), ByteSequence.fromString("password3")).get();

//        authClient.roleGrantPermission(ByteSequence.fromString("rw_" + "appName3_"), ByteSequence.fromString("appName3"), ByteSequence.fromString("*"), Permission.Type.READWRITE);
//        authClient.userGrantRole(ByteSequence.fromString("appName3"), ByteSequence.fromString("rw_" + "appName3_"));

//        authClient.userRevokeRole(ByteSequence.fromString("appName3"), ByteSequence.fromString("rw_" + "appName3_")).get();
//        authClient.roleDelete(ByteSequence.fromString("rw_" + "appName3_")).get();
//        authClient.roleAdd(ByteSequence.fromString("rw_" + "appName3_")).get();
//        authClient.roleGrantPermission(ByteSequence.fromString("rw_" + "appName3_"), ByteSequence.fromString("/appName3/"), ByteSequence.fromString("/appName30"), Permission.Type.READWRITE);
//        authClient.userGrantRole(ByteSequence.fromString("appName3"), ByteSequence.fromString("rw_" + "appName3_"));

        AuthUserGetResponse appName3 = authClient.userGet(ByteSequence.fromString("appName3")).get();
        appName3.getRoles();
        AuthRoleGetResponse role3 = authClient.roleGet(ByteSequence.fromString("rw_" + "appName3_")).get();
        role3.getPermissions();

        System.err.println("完成！");
    }
}